package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.domain.Express;
import com.example.demo.domain.ExpressItem;
import com.example.demo.domain.Order;
import com.example.demo.service.ExpressService;
import com.example.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @Create:     LT4
 * @Time:       2020年6月1日21:41:06
 * @Describe:   用于查询express物流快递信息
 *  8 超时单 9 签收失败 10 退回
 */
@Controller
public class ExpressController {

    @Autowired
    private ExpressService expressService;

    @Autowired
    private OrderService orderService;


    @GetMapping("/show/express/details/{id}")
    public ModelAndView queryExpressInfo(@PathVariable("id") Integer id, ModelAndView modelAndView){
        System.out.println(id);
        Order order=orderService.getOrderById(id);
        JSONObject info=expressService.queryExpressInfo(order.getExpress());
        //th无法进行多级map处理 需要提取关键物流信息
        Map map=JSONObject.parseObject(info.get("showapi_res_body").toString());
        List<ExpressItem> expresslist= JSONArray.parseArray(map.get("data").toString(),ExpressItem.class);
        modelAndView.addObject("expresslist",expresslist);

        //0：购物车  1：支付   2：后台同意   3：收货   4：退货    5：后台同意
        modelAndView.addObject("orderId",order.getId());
        modelAndView.addObject("state",order.getState());// 处于后台同意 并待收货状态

        //将order 的物流信息迁移到
        modelAndView.setViewName("/show/express/details");
        return modelAndView;
    }


    @ResponseBody
    @GetMapping("/get/{id}")
    public Express getExpressById(@PathVariable("id") Integer id){
        return expressService.getExpressById(id);
    }

    @ResponseBody
    @DeleteMapping("del/{id}")
    public String delExpressById(@PathVariable("id") Integer id){
        expressService.delExpressById(id);
        return "del express success";
    }


    @ResponseBody
    @PostMapping("/add")
    public String getExpressById(@RequestBody Express express){
        expressService.addExpress(express);
        return "add express success";
    }

    @ResponseBody
    @PutMapping("/save/")
    public String saveExpress(@RequestBody Express express){
        expressService.saveExpress(express);
        return "save express success";
    }

    @ResponseBody
    @GetMapping("/info")
    public JSONObject queryExpressInfo(@RequestParam("com") String com,@RequestParam("nu") String nu){
        System.out.println(com+" "+nu);
        Express express=new Express();
        express.setCom(com);
        express.setNu(nu);
        return expressService.queryExpressInfo(express);
    }



}
